#include "EnemyManager.h"
#include "Enemy.h"
#include "EasyEnemy.h"
#include "PatrolEnemy.h"
#include "BossEnemy.h"
#include "Utils\Defines.h"
#include "XML\XMLTreeNode.h"

CEnemyManager::CEnemyManager(void)
{
}


CEnemyManager::~CEnemyManager(void)
{
	Destroy();
}

void
CEnemyManager::Init(const std::string &filepath)
{
	CXMLTreeNode newFile;
	if (!newFile.LoadFile(filepath.c_str()))
	{
		printf("ERROR loading the file.");
		return;
	}

	CXMLTreeNode m = newFile["enemies"];
	if (m.Exists())
	{
		int count = m.GetNumChildren();
		for (int i = 0; i < count; ++i)
		{
			if (m(i).GetName() == "core_enemy")
			{
				const char* type = m(i).GetPszProperty("type");

				m_CoreInfo.insert(std::pair<std::string, CXMLTreeNode>(type, m(i)));

			}
			if (m(i).GetName() == "enemy")
			{
				const char* type = m(i).GetPszProperty("type");
			
				if (type == "easy")
					m_Enemy_vector.push_back(new CEasyEnemy(m(i)));
				if (type == "patrol")
					m_Enemy_vector.push_back(new CPatrolEnemy(m(i)));
				if (type == "boss")
					m_Enemy_vector.push_back(new CBossEnemy(m(i)));
			}
		}
	}

	m_filepath = filepath;
}

const CXMLTreeNode &
CEnemyManager::GetCoreEnemy(const std::string &type)
{
	return m_CoreInfo[type];
}

void
CEnemyManager::Destroy()
{
	std::vector<CEnemy*>::iterator it_enemy = m_Enemy_vector.begin();
	std::vector<CEnemy*>::iterator it_enemy_end = m_Enemy_vector.end();

	for( ; it_enemy != it_enemy_end; ++it_enemy)
	{
		CHECKED_DELETE(*(it_enemy));
	}

	m_Enemy_vector.clear();
}

void
CEnemyManager::Reload()
{
	Destroy();
	Init(m_filepath);
}

CEnemyManager*
CEnemyManager::GetInstance()
{
	if (!m_Instance)
		m_Instance = new CEnemyManager();
	return m_Instance;
}

void
CEasyEnemy::Update()
{

}

void
CEasyEnemy::Render()
{

}